import { Component, OnDestroy, OnInit } from '@angular/core';
import { EzFormComponent } from '../ez-form';
import { EzFormControlComponent } from '../ez-form-control';
import { EzFormGroupComponent } from '../ez-form-group';
import { EzFormItemComponent } from '../ez-form-item';
import { EzNumber } from './ez-number';
import { EzNumberConfiguration } from './ez-number.configuration';
@Component({
  selector: 'ez-number',
  templateUrl: './ez-number.component.html',
  styleUrls: ['./ez-number.component.less'],
  providers: [{ provide: EzFormControlComponent, useExisting: EzNumberComponent }],
})
export class EzNumberComponent extends EzFormControlComponent<EzNumberConfiguration, number>
  implements EzNumber, OnInit, OnDestroy {
  defaultMax = Infinity;

  defaultMin = -Infinity;

  defaultStep = 1;

  defaultPrecision = null;

  constructor(
    protected ezForm: EzFormComponent,
    protected ezFormGroup: EzFormGroupComponent,
    protected ezFormItem: EzFormItemComponent
  ) {
    super(ezForm, ezFormGroup, ezFormItem);
  }

  ngOnInit(): void {
    super.ngOnInit();
  }

  ngOnDestroy(): void {
    super.ngOnDestroy();
  }

  checkConfiguration(configuration: any): void {}

  onBlur(event: KeyboardEvent): void {
    if (this.ezFormControlConfiguration.onBlur) {
      this.ezFormControlConfiguration.onBlur(this, this.ezFormGroup, this.ezForm);
    }
  }

  onFocus(event: KeyboardEvent): void {
    if (this.ezFormControlConfiguration.onFocus) {
      this.ezFormControlConfiguration.onFocus(this, this.ezFormGroup, this.ezForm);
    }
  }

  formatter: (value: string | number) => string | number = value => {
    if (this.ezFormControlConfiguration.formatter) {
      return this.ezFormControlConfiguration.formatter(value, this, this.ezFormGroup, this.ezForm);
    }
    return value;
  };
}
